//
//  UIDevice+Extension.swift
//  App
//
//  Created by x on 2024/10/26.
//

import UIKit

public let ScreenWidth = UIScreen.main.bounds.size.width
public let ScreenHeight = UIScreen.main.bounds.size.height
public let ScreenScale = UIScreen.main.scale

//iphonex长宽
public let IphoneXWidth = 375.0
public let IphoneXHeight = 812.0

//公司设计稿比例
public let UIScreenWidth = IphoneXWidth
public let UIScreenHeight = IphoneXHeight

//公司屏幕缩放比
public let ScreenWidthScale = ScreenWidth / UIScreenWidth
public let ScreenHeightScale = ScreenHeight / UIScreenHeight

public func XWidth(_ width: CGFloat) -> CGFloat {
    return CGFloat(Int(width * ScreenWidthScale))
}

public func XHeight(_ height: CGFloat) -> CGFloat {
    return CGFloat(Int(height * ScreenHeightScale))
}
//屏幕比例
public let XPixel = 1.0 / UIScreen.main.scale

public let NavigationBarHeight = UIDevice.NavigationBarHeight
public let StatusBarHeight = UIDevice.StatusBarHeight
public let TopSafeHeight = UIDevice.TopSafeHeight
public let TopSafeHeightAndNaviBarHeight = UIDevice.TopSafeHeightAndNaviBarHeight
public let TabbarSafeAreaHeight = UIDevice.TabbarSafeAreaHeight
public let TabbarHeight = UIDevice.TabbarHeight

public extension UIDevice {
    
    static var isPad: Bool {
        if #available(iOS 14.0, *), ProcessInfo.processInfo.isiOSAppOnMac {
            return true
        }
        return current.userInterfaceIdiom == .pad
    }
    
    static var isPortrait: Bool {
        if #available(iOS 14.0, *), ProcessInfo.processInfo.isiOSAppOnMac {
            return true
        }
        if isPad {
            return true
        }
        let statusBarOrientation = UIApplication.interfaceOrientation
        if  statusBarOrientation == .landscapeLeft ||
            statusBarOrientation == .landscapeRight {
            return false
        }
        return true
    }
    /// 顶部安全区域
    static var TopSafeHeight: CGFloat {
        var topSafeHeight: CGFloat = 0.0
        if #available(iOS 13.0, *) {
            if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
                guard let window = windowScene.windows.first, window.isKeyWindow else { return 0.0 }
                topSafeHeight = window.safeAreaInsets.top
            }
        } else {
            guard let window = UIApplication.shared.windows.first, window.isKeyWindow else { return 0.0 }
               topSafeHeight = window.safeAreaInsets.top
        }
        return topSafeHeight
    }
    /// 导航栏高度(分iPad 和 iOS)
    static var NavigationBarHeight: CGFloat {
        if #available(iOS 12, *), isPad {
            return  50
        }
        return 44
    }
    
    /// 顶部状态栏高度(带安全区域)
    static var StatusBarHeight: CGFloat {
        if #available(iOS 13.0, *) {
            // iOS 13 及以上
            if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
                return windowScene.statusBarManager?.statusBarFrame.height ?? 20.0
            }
            // 如果没有获取到窗口场景
            return UIApplication.shared.windows.first?.safeAreaInsets.top ?? 0.0
        } else {
            // iOS 12 及以下
            // 防止界面没有出来获取为0的情况
            return UIApplication.shared.statusBarFrame.height > 0 ? UIApplication.shared.statusBarFrame.height : 44
        }
    }
    
    /// 顶部状态栏 + 导航栏高度
    static var TopSafeHeightAndNaviBarHeight: CGFloat {
        return StatusBarHeight + NavigationBarHeight
    }

    static var TabbarSafeAreaHeight: CGFloat {
        var safeHeight: CGFloat = 0.0
        
        if #available(iOS 13.0, *) {
            if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
                guard let window = windowScene.windows.first, window.isKeyWindow else { return 0.0 }
                safeHeight = window.safeAreaInsets.bottom
            }
        } else {
            guard let window = UIApplication.shared.windows.first, window.isKeyWindow else { return 0.0 }
            safeHeight = window.safeAreaInsets.bottom
        }
        return safeHeight
    }
    /// 标签栏Tabbar高度
    static var TabbarHeight: CGFloat {
        return TabbarSafeAreaHeight + 49.0;
    }
    
    
}

extension UIDevice: XExtensionWarapper {}
